package com.patchmanage.back.controller;


import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.patchmanage.back.annotation.OperLog;
import com.patchmanage.back.entity.SafeConfig;
import com.patchmanage.back.exception.SystemException;
import com.patchmanage.back.result.CodeMsg;
import com.patchmanage.back.result.Result;
import com.patchmanage.back.service.ISafeConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huizhao
 * @since 2021-04-14
 */
@Api(value = " 系统安全 controller", tags = {"系统安全 Api"})
@RestController
@RequestMapping("/back/safeconfig")
public class SafeConfigController {
    @Autowired
    public ISafeConfigService safeConfigService;

    /**
     * 获取安全配置接口
     *
     * @param dict
     * @return
     */
    @ApiOperation(value = "获取安全配置更新接口", notes = "获取安全配置更新接口")
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer", paramType = "body")
    })
    public Result<SafeConfig> getById(@ApiIgnore @RequestBody Dict dict) {
        return Result.success(safeConfigService.getById(dict.getInt("id")));
    }


    /**
     * 新增安全配置更新接口
     * @param safeConfig
     * @return
     */
    @ApiOperation(value = "新增安全配置更新接口", notes = "新增安全配置更新接口")
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    @OperLog(operModul="系统安全",operDesc="新增安全配置")
    public Result<Boolean> saveOrUpdate(@RequestBody SafeConfig safeConfig){
        return Result.success(safeConfigService.saveOrUpdate(safeConfig));
    }


    /**
     * 密码安全策略设置
     * @param dict
     * @return
     */
    @ApiOperation(value = "密码安全策略设置", notes = "密码安全策略设置")
    @RequestMapping(value = "passwordSafeVerification", method = RequestMethod.POST)
    @OperLog(operModul="系统安全",operDesc="密码安全策略设置")
    public Result<Boolean> passwordSafeVerification(@ApiIgnore @RequestBody Dict dict){

        String password  = dict.getStr("password");

        SafeConfig safeConfig = safeConfigService.getById(1);
        //最大长度检验
        Integer maxCharacterNum = safeConfig.getMaxCharacterNum();
        if(maxCharacterNum != null){
            if(password.length() > maxCharacterNum){
                //长度过大
                throw new SystemException(CodeMsg.PASSWORD_VERIFICATION_MAXCHARACTER_ERROR);
            }
        }

        //最小长度校验
        Integer minCharacterNum =safeConfig.getMinCharacterNum();
        if(minCharacterNum != null){
            if(password.length() < minCharacterNum){
                //长度过小
                throw new SystemException(CodeMsg.PASSWORD_VERIFICATION_MINCHARACTER_ERROR);
            }
        }

        //最小长度校验
        String mustIncludCharacterType = safeConfig.getMustIncludCharacterType();
        if(StrUtil.isNotBlank(mustIncludCharacterType)){
            String[] mustIncludCharacterTypeArray = mustIncludCharacterType.split(".");
            for(String mustIncludCharacterTypeStr : mustIncludCharacterTypeArray){
                if(mustIncludCharacterTypeStr.equals("大写字母") && !(ReUtil.isMatch("[A-Z]",password))){
                    throw new SystemException(CodeMsg.PASSWORD_VERIFICATION_INCLUD_BIG_ERROR);

                }else if(mustIncludCharacterTypeStr.equals("小写字母") && !(ReUtil.isMatch("[a-z]*",password))){
                    throw new SystemException(CodeMsg.PASSWORD_VERIFICATION_INCLUD_SMAIL_ERROR);

                }else if(mustIncludCharacterTypeStr.equals("数字") && !(ReUtil.isMatch("[0-9]*",password))){
                    throw new SystemException(CodeMsg.PASSWORD_VERIFICATION_INCLUD_NUMBER_ERROR);

                }else if(mustIncludCharacterTypeStr.equals("特殊字符") && !(ReUtil.isMatch("[~!@#$^&*.]",password))){
                    throw new SystemException(CodeMsg.PASSWORD_VERIFICATION_INCLUD_SPECIAL_ERROR);
                }
            }

        }
        return Result.success(true);
    }
}
